package com.itheima.d7_thread_pool;

import java.util.concurrent.*;

/**
 * 目标：掌握线程池的创建
 * |---线程池执行Runnable任务执行
 */
public class ThreadPoolTest1 {
    public static void main(String[] args) {
        // 1、通过ThreadPoolExecutor创建一个线程池对象参考
    /*    ExecutorService pool = new ThreadPoolExecutor(
                3, //核心线程数
                5,//最大线程数
                8,//存活时间
                TimeUnit.SECONDS,//时间单位
                new ArrayBlockingQueue<>(4),//队列
                Executors.defaultThreadFactory(),//线程工厂
                new ThreadPoolExecutor.CallerRunsPolicy()//拒绝策略
        );
    */
        ExecutorService pool = new ThreadPoolExecutor(3, 5, 8, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(4), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()
        );

        for (int i = 0; i < 10; i++) {
            pool.execute(()->{
            final String name = Thread.currentThread().getName();
                System.out.println(name+"::HELLO  薛正卿");

                try{
                    Thread.sleep(300);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            });
        }
        //关闭线程池，一般线程池是不会关闭的，除非确定之后不会再使用
        pool.shutdown();
    }
}
